<?php

namespace App\Models;

use App\Models\Field\ArticleField;

/**
 * App\Models\SysArticle
 * This is the model class for table "sys_article_".
 *
 * @property int $id 自增长ID
 * @property string $article_id 文章ID
 * @property string $author_id 作者ID
 * @property int $spider_original_id 对应[spider_original]表ID
 * @property string $original_url 对应[spider_info]表url
 * @property string $cover_url 封面图地址
 * @property string $title 标题
 * @property string $description 摘要
 * @property int $type 类型，1推荐，2原创
 * @property int $status 标签（如img、video等）替换是否完成，0未完成，1完成
 * @property int $is_deleted 删除状态，0正常，1删除
 * @property int $is_shield 屏蔽状态，0正常，1屏蔽
 * @property int $original_created_at 第三方平台发布时间戳
 * @property int $created_at 创建时间戳
 * @property int $updated_at 更新时间戳
 *
 * @method static \Illuminate\Database\Eloquent\Builder|SysArticle newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SysArticle newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SysArticle query()
 *
 * @property SysArticleContent $content
 * @property SysArticleContent $contentModel
 * @property User $author
 * @property IndexArticle $index
 */
class SysArticle extends BaseModel
{

    protected $table = "sys_article_";
    protected $primaryKey = "article_id";
    protected $keyType = "string";
    public $timestamps = true;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'created_at' => 'timestamp',
    ];

    /**
     * select columns
     * @var array
     */
    public $fields = [
        'search' => ['article_id as id', 'author_id', 'cover_url', 'title', 'description', 'is_deleted', 'is_shield', 'created_at', 'original_url', 'spider_original_id'],
        'content' => ['article_id', 'content'],
        'collect' => ['id', 'article_id', 'author_id', 'title', 'description', 'cover_url', 'is_deleted', 'is_shield', 'created_at'],
    ];

    /**
     * 关联模型 - 文章内容
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function content()
    {
        return $this->hasOne(SysArticleContent::class, 'article_id', 'article_id', $this->article_id)
            ->select($this->fields['content']);
    }

    public function contentModel()
    {
        return $this->hasOne(SysArticleContent::class, 'article_id', 'article_id', $this->article_id);
    }

    /**
     * 模型属性 - 文章内容
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getContentAttribute()
    {
        return SysArticleContent::getInstance($this->article_id, true)
            ->select($this->fields['content'])
            ->value('content');
    }

    public function getAuthorAttribute()
    {
        return User::getInstance()
            ->where('user_id', $this->author_id)
            ->first();
    }

    public function getIndexAttribute()
    {
        return IndexArticle::getInstance()
            ->where('article_id', $this->article_id)
            ->first();
    }

    public function commonFormat($scene = ArticleField::BASE_INFO): array
    {
        $formats = ArticleField::getFormats($scene);
        return ArticleField::format($this, $formats);
    }
}
